package com.rabbitmq.demo.service.impl;

import com.rabbitmq.demo.entity.User;
import com.rabbitmq.demo.service.IRabbitMQService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
public class RabbitMQServiceImpl implements IRabbitMQService {
// 以下两个方法是基于API方式，偶尔使用，但是容易和业务代码产生耦合
    // 监听消息队列名称为"fanout_queue_email"的消息
    @RabbitListener(queues = "fanout_queue_email")
    public void psubConsumerEmail(Message message){
        byte[] body = message.getBody();
        String s = new String(body);
        System.out.println("邮件业务接收到消息：" + s);
    }


    // 监听消息队列名称为"fanout_queue_sms"的消息
    @RabbitListener(queues = "fanout_queue_sms")
    public void psubConsumerSms(Message message){
        byte[] body = message.getBody();
        String s = new String(body);
        System.out.println("短信业务接收到消息：" + s);
    }

    //以下两个方法是基于注解的方式，较为常见，但是也容易与业务代码产生耦合
    // bindings用于定制并绑定交换器和消息队列组件，为了能使消费者接收到实体类User，需要在定制交换器时将交换器的类型设置为fanout
    @RabbitListener(bindings = @QueueBinding(value = @Queue("fanout_queue_email"), exchange = @Exchange(value = "fanout_exchange",type = "fanout"), key = {""}))
    public void psubConsumerEmailAno(User user){
        System.out.println("邮件业务接收到消息：" + user);
    }

    // bindings用于定制并绑定交换器和消息队列组件，为了能使消费者接收到实体类User，需要在定制交换器时将交换器的类型设置为fanout
    @RabbitListener(bindings = @QueueBinding(value = @Queue("fanout_queue_sms"), exchange = @Exchange(value = "fanout_exchange", type = "fanout"), key = {""}))
    public void psubConsumerSmsAno(User user){
        System.out.println("短信业务接收到消息：" + user);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("routing_queue_error"), exchange = @Exchange(value = "routing_exchange", type = "direct"), key = {"error_routing_key"}))
    public void routingConsumerError(String message){
        System.out.println("接收到error级别日志消息:" + message);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("routing_queue_all"), exchange = @Exchange(value = "routing_exchange", type = "direct"), key = {"error_routing_key", "info_routing_key", "warning_routing_key"}))
    public void routingConsumerAll(String message){
        System.out.println("接收到info、error、warning等级别日志消息:" + message);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("topic_queue_email"), exchange = @Exchange(value = "topic_exchange", type = "topic"), key = "info.#.email.#"))
    public void topicConsumerEmail(String message){
        System.out.println("接收到邮件订阅需求处理消息：" + message);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("topic_queue_sms"), exchange = @Exchange(value = "topic_exchange", type = "topic"), key = "info.#.sms.#"))
    public void topicConsumerSms(String message){
        System.out.println("接收到短信订阅需求处理消息：" + message);
    }
}
